﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.Services/AppCloseProvider
* 类       名: AppCloseProvider
* 功      能:  TD
* 时      间:  2024-05-08 11:34:31
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Microsoft.Identity.Client;
using Microsoft.IdentityModel.Logging;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WaferSelectV3.Common;
using WaferSelectV3.EventsManager;

namespace WaferSelectV3.Services
{
    /// <summary>
    /// 程序退出帮助类,一般出现普通异常的时候要退出程序,退出程序的时候要做的事情
    /// </summary>
    public class AppCloseProvider
    {
        private static IEventAggregator? _eventAggregator;

        public static void SetEventAggregator(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        /// <summary>
        /// 发生异常需要退出程序的时候调用这个程序
        /// </summary>
        /// <param name="errorMsg"></param>
        public static void CloseAppWithErrorHappen(string errorMsg)
        {
            // 发布程序关闭事件
            LoggerHelper.WriteLogMain(errorMsg + " 关闭程序", LogType.Fatal);
            MessageBox.Show(errorMsg, "程序出现异常提示框", MessageBoxButton.OK, MessageBoxImage.Error);
            _eventAggregator!.GetEvent<AppCloseEvent>().Publish();
            Environment.Exit(0);
        }
    }
}
